package com.lsk.mps.backend.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Slf4j
@Component
public class SubscribeThreadStarter implements ApplicationRunner {
    @Autowired
    private JedisPool jedisPool;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try (Jedis jedis = jedisPool.getResource()){
                    jedis.subscribe(new Subscriber(), "message");
                } catch (Exception e) {
                    log.error("Subscribe error: ", e);
                }
            }
        }).start();
    }
}
